import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { isEmpty } from "@/utils/validate"
import { loginApi } from '@/api/login/login-api'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { showConfirm,toast,wxLogin  } from '@/utils/common'; 


const user = {
  state: {
    token: getToken(),
    realName: storage.get(constant.realName),
    avatar: storage.get(constant.avatar),
    mobile: storage.get(constant.mobile),
	points: storage.get(constant.points),
	orgName: storage.get(constant.orgName),
	orgId: storage.get(constant.orgId),
	memberId: storage.get(constant.memberId),
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_REALNAME: (state, realName) => {
      state.realName = realName
      storage.set(constant.realName, realName)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
    SET_MOBILE: (state, mobile) => {
      state.mobile = mobile
      storage.set(constant.mobile, mobile)
    },
	SET_POINTS: (state, points) => {
	  state.points = points
	  storage.set(constant.points, points)
	},
	SET_ORGNAME: (state, orgName) => {
		state.orgName = orgName
		storage.set(constant.orgName, orgName)
	},
	SET_ORGID: (state, orgId) => {
		state.orgId = orgId
		storage.set(constant.orgId, orgId)
	},
	SET_MEMBERID: (state, memberId) => {
		state.memberId = memberId
		storage.set(constant.memberId, memberId)
	},
  },

  actions: {
    // 登录
    Login({ commit,dispatch }, userInfo) {
      return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		loginApi.loginByCode(userInfo).then(res => {
			setToken(res.data.access_token);
			commit('SET_TOKEN', res.data.access_token);
			resolve();
		}).catch(error => {
		  reject(error)
		})
		// #endif  
		
		// #ifdef H5
		loginApi.loginByUnionId(userInfo).then(res => {
			setToken(res.data.access_token);
			commit('SET_TOKEN', res.data.access_token);
			resolve();
		}).catch(error => {
		  reject(error)
		})
		// #endif
        
      })
    },
	LoginByUnionId({ commit,dispatch }, userInfo) {
	  return new Promise((resolve, reject) => {
		loginApi.loginByUnionId(userInfo).then(res => {
			setToken(res.data.access_token);
			commit('SET_TOKEN', res.data.access_token);
			resolve();
		}).catch(error => {
		  reject(error)
		})
	  })
	},
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        loginApi.getInfo().then(res => {
          const user = res.data
		  let avatar = user.avatar || ""
		  
          const realName = (isEmpty(user) || isEmpty(user.realName)) ? "" : user.realName
		  const mobile = user.mobile;
		  const points = user.points;
		  const orgName = user.orgName;
		  const orgId = user.orgId;
		  const memberId = user.memberId;
          commit('SET_REALNAME', realName)
          commit('SET_AVATAR', avatar)
		  commit('SET_MOBILE', mobile)
		  commit('SET_POINTS', points)
		  uni.setStorageSync('userInfo', user)
		  commit('SET_ORGNAME', orgName)
		  commit('SET_ORGID', orgId)
		  commit('SET_MEMBERID', memberId)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
	UpdateInfo({commit,state},userInfo) {
		return new Promise((resolve, reject) => {
			const {avatar,realName,mobile,points,orgName,orgId,memberId} = userInfo;
			commit('SET_REALNAME', realName)
			commit('SET_AVATAR', avatar)
			commit('SET_MOBILE', mobile)
			commit('SET_POINTS', points)
			commit('SET_ORGNAME', orgName)
			commit('SET_ORGID', orgId)
			commit('SET_MEMBERID', memberId)
			uni.setStorageSync('userInfo', userInfo);
			resolve({});
		}).catch(error => {
          reject(error)
        })	
		
	},

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        // loginApi.logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   removeToken()
        //   storage.clean()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
		commit('SET_TOKEN', '')
		removeToken()
		storage.clean()
      })
    }
  }
}

export default user